<template>
  <div class="articleList">
    <van-pull-refresh success-duration= "1000" :success-text="Tips" v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <!-- <van-cell v-for="item in list" :key="item.art_id" :title="item.title" /> -->
        <ArticItem :row = 'item' v-for="item in list" :key="item.art_id + ''"/>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticeList } from '@/api/article.js'
import ArticItem from '@/components/ArticItem.vue'
export default {
  props: {
    channalId: {
      type: Number,
      require: true
    }
  },
  components: {
    ArticItem
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: Date.now(),
      error: false,
      isLoading: false,
      Tips: ''

    }
  },

  created () {

  },

  methods: {
    async onLoad () {
      try {
        const res = await getArticeList({
          channel_id: this.channalId,
          timestamp: this.timestamp,
          with_top: 0
        })
        // 随机数测试失败
        if (Math.random() > 0.5) {
          console.lg(66)
        }
        // 保存数据
        this.list.push(...res.results)
        // 更新时间戳
        this.timestamp = res.pre_timestamp
        // 让当前加载状态结束
        this.loading = false
        // 加载完成
        if (!res.loading.length) {
          this.finished = true
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
    },
    async onRefresh () {
      try {
        const res = await getArticeList({
          channel_id: this.channalId,
          timestamp: Date.now(),
          with_top: 1
        })
        // 随机数测试失败
        if (Math.random() > 0.5) {
          console.lg(66)
        }
        this.list.unshift(...res.results)
        this.Tips = '刷新成功'
        this.isLoading = false
      } catch (err) {
        this.Tips = '刷新失败,请检查网络'
        this.isLoading = false
      }
    }

  }
}
</script>

<style scoped lang='less'>
.articleList {
  height: 80vh;
  overflow-y: scroll;

}
</style>
